<?php

namespace IDock\Insurance\JDAL\Request;

use IDock\Http\Request;
use IDock\Http\RequestFormat;
use IDock\Http\RequestMethod;
use IDock\Insurance\JDAL\Response\AllJDALResponse;

class AllJDALRequest extends Request
{

    protected $method = RequestMethod::POST;

    protected $format = RequestFormat::JSON;

    private $sign;

    protected $responseLog = true;

    public $route = '';

    public $requestCode;

    public $businessNo;

    public $partnerRef;

    public $callBackUrl;

    public function loadConfig(array $config): Request
    {
        $this->gateway = $config['gateway'] ?? '';

        $this->logsDir = $config['logsDir'] ?? '';

        $this->timeout = $config['timeout'] ?? '';

        $this->proxy = $config['proxy'] ?? '';

        $this->proxyPW = $config['proxyPW'] ?? '';

        $this->proxyAccount = $config['proxyAccount'] ?? '';

        $this->callBackUrl = $config['callbackUrl'] ?? '';

        return $this;
    }

    public function setSign($sign): void
    {
        $this->sign = $sign;
    }

    public function getSign()
    {
        return $this->sign;
    }

    public function requestLogs($uuid, $inLog = true)
    {
        if (!$this->logsFile) {
            return;
        }

        if ($this->urlTip) {
            $url = $this->gateway;
            $urlLogs = date('Y-m-d H:i:s') . " FUN_TIPS " . $uuid . " " . $this->urlTip . ' ' . $url;
        } else {
            $urlLogs = date('Y-m-d H:i:s') . " URL      " . $uuid . " " . $this->gateway;
        }

        $isTest = $GLOBALS['testResponse'] ?? '';

        if ($isTest and $urlLogs) {
            file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);

            return;
        }

        file_put_contents($this->logsFile, "\r\n", FILE_APPEND);

        if ($urlLogs) {
            file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);
        }

        if (!$inLog) {
            return;
        }

        if ($this->headers) {
            $encLogs = date('Y-m-d H:i:s') . " HEADER   " . $uuid . " " . json_encode($this->getHeaders(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if ($this->preParameter != $this->bizParam) {
            $encLogs = date('Y-m-d H:i:s') . " ENCRYPT  " . $uuid . " " . json_encode($this->preParameter, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        $biz = '';

        if ($this->bizParam ?? '') {
            $biz = json_encode($this->bizParam, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        }
        $reqLogs = date('Y-m-d H:i:s') . " REQUEST  " . $uuid . " " . $biz;
        file_put_contents($this->logsFile, $reqLogs . "\r\n", FILE_APPEND);
    }

    public function responseLogs($uuid, $data)
    {

    }

    public function responseAfterLogs($data,$isSuccess)
    {
        if (!$this->logsFile) {
            return;
        }

        if (!$this->inLog) {
            return;
        }

        $uuid = $this->getRequestId();

        if ($isSuccess and !$this->responseLog) {
            return;
        }

        if (is_array($data)) {
            $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        }

        $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . $data;

        file_put_contents($this->logsFile, $repLogs . "\r\n", FILE_APPEND);
    }

    public function decryptLogs($uuid, $data)
    {
        if (!$data) {
            return;
        }

        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $decLogs = date('Y-m-d H:i:s') . " DECRYPT  " . $uuid . " " . $data;
        file_put_contents($this->logsFile, $decLogs . "\r\n", FILE_APPEND);
    }
}
